package com.baiye.template.dao;

import com.baiye.template.pojo.Permission;
import com.baiye.template.pojo.Role;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author GUOZHIPENG
 * @version 1.0
 * @date 2021/9/7 16:50
 */
@Repository
public interface PermissionDao {

    @Delete("delete from permission where id = #{id}")
    int deleteById(int id);

    @Insert("insert into permission(name,url,desc,creat_time) values(#{name},#{url},#{desc},#{creatTime})")
    int add(Permission permission);

    @Insert("insert into role_permission(role_id,permission_id) values(#{roleId},#{permissionId})")
    int addPermissionToRole(@Param("roleId") int roleId, @Param("permissionId")int permissionId);

    @Insert("delete from role_permission where role_id=#{roleId} and permission_id=#{permissionId}")
    int deletePermissionToRole(@Param("roleId") int roleId, @Param("permissionId")int permissionId);

    @Select({"<script>" +
            "select * from permission " +
            " <where> " +
//            "<if test='start!=null and size !=null'>id >=( select id from permission order by id limit #{start},1 )</if>"+
            " <if test='permission.id != null'>AND id=#{permission.id}</if> " +
            " <if test='permission.name != null'> AND name=#{permission.name}</if> " +
            " <if test='permission.url != null'> AND locate (#{permission.url} , url) > 0</if> " +
            " </where> " +
            "</script>"})
    List<Permission> getPermission(@Param("permission")Permission permission, @Param("start") Integer start, @Param("pageSize") Integer pageSize);

    @Select("select * from permission where id in (select permission_id from role_permission where role_id=#{roleId})")
    List<Permission> getPermissionByRoleIdExist(Integer roleId);

    @Select("select * from permission where id not in (select permission_id from role_permission where role_id=#{roleId})")
    List<Permission> getPermissionByRoleIdNotExist(Integer roleId);

    @Update("update permission set `name`=#{name},`url`=#{url},`desc`=#{desc} where id=#{id}")
    int updatePermission(Permission permission);
    @Select("select * from role where id in (select role_id from role_permission where permission_id=(select id from permission where `url`=#{requestUrl}))")
    List<Role> selectListByPath(String requestUrl);
}
